package com.itboye.lottery.activity

import android.view.View
import android.widget.RelativeLayout
import android.widget.TextView
import com.itboye.lottery.R
import com.itboye.lottery.alert.ByAlert
import com.itboye.lottery.base.BaseActivity
import com.itboye.lottery.presenter.UserPresenter
import com.itboye.lottery.retrofit.ResponseCallback
import com.itboye.lottery.utils.Flavor
import com.itboye.lottery.utils.TimeCount
import com.itboye.lottery.utils.Utils
import kotlinx.android.synthetic.main.activity_login.*

class RegisterActivity : BaseActivity(), ResponseCallback {


    var rl_wx_login: RelativeLayout? = null
    var userPresenter: UserPresenter? = null
    var tv_get_yzm: TextView? = null
    var timeCount: TimeCount? = null
    var tv_register: TextView? = null
    override fun getLayoutId(): Int {
        return R.layout.activity_login
    }

    override fun initData() {
        userPresenter = UserPresenter(this)
        timeCount = TimeCount(30000, 1000, tv_get_yzm)
        iv_logo.setBackgroundResource(Flavor().logo)
        tv_submit.text = "注册"
        tv_register!!.visibility = View.GONE
        rl_invite_code.visibility = View.VISIBLE
    }

    override fun onClick(p0: View?) {
        when (p0!!.id) {
            R.id.rl_wx_login -> {
                //手机号码注册
                var phone = et_phone.text.toString()
                var code = et_code.text.toString()
                var inviteCode = et_invite_code.text.toString()
                if (!Utils.emptyMsgShow(phone, "手机号码")) return
                if (!Utils.emptyMsgShow(code, "验证码")) return
                if (!Utils.emptyMsgShow(inviteCode, "邀请码")) return
                userPresenter!!.registerByPhone(phone, code, inviteCode)
            }
            R.id.tv_get_yzm -> {
                //获取验证码
                var phone = et_phone.text.toString()
                if (!Utils.emptyMsgShow(phone, "手机号码")) return
                timeCount!!.start()
                showDialog("正在请求发送验证码")
                userPresenter!!.sendCode(5, phone)
            }
            else -> {
            }
        }
    }

    override fun success(o: Any, requestSuccessType: String) {
        dissDialog()
        if (requestSuccessType == userPresenter!!.registerByPhone_SUCCESS) {
//            var userDataBean: UserDataBean = o as UserDataBean
            ByAlert.alert("注册成功,请重新登录")
            finish()
        } else if (requestSuccessType == userPresenter!!.sendCode_SUCCESS) {
            ByAlert.alert(o)
        }
    }

    override fun fail(code: Int, msg: Any, requestErrorType: String) {
        dissDialog()
        if (requestErrorType === userPresenter!!.registerByPhone_FAIL) {
            ByAlert.alert(msg)
        } else if (requestErrorType === userPresenter!!.sendCode_FAIL) {
            ByAlert.alert(msg)
        }
    }
}
